/**
  HomeMade by shoe[box]
 
  Redistribution and use in source and binary forms, with or without 
  modification, are permitted provided that the following conditions are
  met:

  * Redistributions of source code must retain the above copyright notice, 
    this list of conditions and the following disclaimer.
  
  * Redistributions in binary form must reproduce the above copyright
    notice, this list of conditions and the following disclaimer in the 
    documentation and/or other materials provided with the distribution.
  
  * Neither the name of shoe[box] nor the names of its 
    contributors may be used to endorse or promote products derived from 
    this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
  IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package org.shoebox.guacamole.core {

	import org.shoebox.guacamole.view.GuaScene;
	import org.shoebox.guacamole.view.abstracts.GuaMesh;
	import org.shoebox.utils.logger.Logger;
	import flash.display3D.IndexBuffer3D;
	import flash.display3D.VertexBuffer3D;
	import flash.utils.Dictionary;
	
	/**
	* org.shoebox.guacamole.core.MeshCacher
	* @author shoebox
	*/
	public class BufferCacher {
		
		protected var _oCacheIndexs				: Dictionary;
		protected var _oCacheNormals				: Dictionary;
		protected var _oCacheUVs				: Dictionary;
		protected var _oCacheVertices			: Dictionary;
		
		// -------o constructor
		
			/**
			* Constructor of the MeshCacher class
			*
			* @public
			* @return	void
			*/
			public function BufferCacher( e : SingletonEnforcer ) : void {
				_oCacheVertices 	= new Dictionary( );
				_oCacheIndexs 	= new Dictionary( );
				_oCacheNormals 	= new Dictionary( );
				_oCacheUVs	 	= new Dictionary( );
			}

		// -------o public
			
			/**
			* getBuffer function
			* @public
			* @param 
			* @return
			*/
			static public function getVertexBuffer( mesh : GuaMesh , scene : GuaScene ) : VertexBuffer3D {
				return getInstance()._getVertexBuffer( mesh.vVerticesCoords , mesh.iBufferVerticesSize , scene );
			}
			
			/**
			* getIndexBuffer function
			* @public
			* @param 
			* @return
			*/
			static public function getIndexBuffer( mesh : GuaMesh , scene : GuaScene ) : IndexBuffer3D {
				return getInstance()._getIndexBuffer( mesh.vIndexs , mesh.iBufferUVsSize , scene );
			}
			
			/**
			* getUVBuffer function
			* @public
			* @param 
			* @return
			*/
			static public function getUVBuffer( mesh : GuaMesh , scene : GuaScene ) : VertexBuffer3D {
				return getInstance()._getUVBuffer( mesh.vUVCoords , mesh.iBufferUVsSize , scene );
			}
			
			
			/**
			* getUVBuffer function
			* @public
			* @param 
			* @return
			*/
			static public function getNormalsBuffer( mesh : GuaMesh , scene : GuaScene ) : VertexBuffer3D {
				return getInstance()._getNormalsBuffer( mesh.vUVCoords , mesh.iBufferUVsSize , scene );
			}
			
			/**
			* reset function
			* @public
			* @param 
			* @return
			*/
			static public function purge() : void {
				getInstance()._purge( );
			}
			
		// -------o protected
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _purge() : void {
				_dispose( _oCacheUVs );
				_dispose( _oCacheIndexs );
				_dispose( _oCacheNormals );
				_dispose( _oCacheVertices );
				return;
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _dispose( d : Dictionary ) : void {
				
				var codeName : String;
				var value : *;
				for( codeName in d ){
					
					value = d[ codeName ];
					
					if( value is IndexEntry)
						( value as IndexEntry ).datas.dispose( );
					
					if( value is CacheEntry)
						( value as CacheEntry ).datas.dispose( );
					
					delete d[codeName];
				}
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _getVertexBuffer( source : Vector.<Number> , iBufferVerticesSize : int , scene : GuaScene ) : VertexBuffer3D {
				
				var sCodeName : String = source.join ( '|' ) + '--' + iBufferVerticesSize;
				var cache : CacheEntry = _oCacheVertices[ sCodeName ];
				
				if( !cache ){
					var iNumVertices 	: int = source.length / iBufferVerticesSize;
					var 	vBuffer : VertexBuffer3D = scene.refContext3D.createVertexBuffer ( iNumVertices , iBufferVerticesSize );
						vBuffer.uploadFromVector ( source , 0 , iNumVertices );
						
					_oCacheVertices[ sCodeName ] = cache = new CacheEntry( source , iBufferVerticesSize , vBuffer );
				}
				
				return cache.datas;
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _getNormalsBuffer( source : Vector.<Number> , iBufferNormalsSize : int , scene : GuaScene ) : VertexBuffer3D {
				var sCodeName : String = source.join ( '|' ) + '--' + iBufferNormalsSize;
				var cache : CacheEntry = _oCacheNormals[ sCodeName ];
				
				if( !cache ){
					var iNumVertices 	: int = source.length / iBufferNormalsSize;
					var 	vBuffer : VertexBuffer3D = scene.refContext3D.createVertexBuffer ( iNumVertices , iBufferNormalsSize );
						vBuffer.uploadFromVector ( source , 0 , iNumVertices );
						
					_oCacheNormals[ sCodeName ] = cache = new CacheEntry( source , iBufferNormalsSize , vBuffer );
				}
				
				return cache.datas;
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _getUVBuffer( source : Vector.<Number> , iBufferUVSize : int , scene : GuaScene ) : VertexBuffer3D {
				
				var sCodeName : String = source.join ( '|' ) + '--' + iBufferUVSize;
				var cache : CacheEntry = _oCacheUVs[ sCodeName ];
				if( !cache ){
					var iNumVertices 	: int = source.length / iBufferUVSize;
					var 	vBuffer : VertexBuffer3D = scene.refContext3D.createVertexBuffer ( iNumVertices , iBufferUVSize );
						vBuffer.uploadFromVector ( source , 0 , iNumVertices );
						
					_oCacheUVs[ sCodeName ] = cache = new CacheEntry( source , iBufferUVSize , vBuffer );
				}
				
				return cache.datas;
			}
			
			/**
			* 
			*
			* @param 
			* @return
			*/
			final protected function _getIndexBuffer( source : Vector.<uint> , iBufferIndexSize : int , scene : GuaScene ) : IndexBuffer3D {
				var sCodeName : String = source.join ( '|' ) + '--' + iBufferIndexSize;
				var cache : IndexEntry = _oCacheIndexs[ sCodeName ];
				
				if( !cache ){
					var 	iNumIndexs: int = source.length;
					var 	vBuffer : IndexBuffer3D = scene.refContext3D.createIndexBuffer( iNumIndexs );
						vBuffer.uploadFromVector ( source , 0 , iNumIndexs );
						
					_oCacheIndexs[ sCodeName ] = cache = new IndexEntry( source , iBufferIndexSize , vBuffer );
				}
				
				return cache.datas;
			}
			
		// -------o misc

			public static function trc(...args : *) : void {
				Logger.log(BufferCacher, args);
			}
			
			/**
			* Return the singleton instance of the class
			* @public
			* @return instance of the class (MeshCacher)
			*/
			static public function getInstance() : BufferCacher {				
				if( !__instance )
					__instance = new BufferCacher( new SingletonEnforcer() );
								
				return __instance;
			}
			
			protected static var __instance			: BufferCacher = null;
	}
}

import flash.display3D.IndexBuffer3D;
import flash.display3D.VertexBuffer3D;

internal class IndexEntry{
	
	public var bufferSize		: int;
	public var raw			: Vector.<uint>;
	public var datas			: IndexBuffer3D;
	
	function IndexEntry( raw : Vector.<uint> , bufferSize : int , datas : IndexBuffer3D ) : void{
		this.raw = raw;
		this.bufferSize = bufferSize;
		this.datas = datas;
	}
		
}


internal class CacheEntry{
	
	public var bufferSize		: int;
	public var raw			: Vector.<Number>;
	public var datas			: VertexBuffer3D;
	
	function CacheEntry( raw : Vector.<Number> , bufferSize : int , datas : VertexBuffer3D ) : void{
		this.raw = raw;
		this.bufferSize = bufferSize;
		this.datas = datas;
	}
		
}

internal class SingletonEnforcer{
	
}
